package com.br4mmie.asciistory.battle;

import java.util.ArrayList;

import com.br4mmie.asciistory.ASCIIStoryActivity;
import com.br4mmie.asciistory.CharImage;
import com.br4mmie.asciistory.characters.Player;
import com.br4mmie.asciistory.ui.OptionWindow;

/**
 * EncounterPlayer class, Acts as the players' representation in battle
 * Extends the EncounterCreature class
 * @author Niels
 *
 */
public class EncounterPlayer extends EncounterCreature implements OptionWindow.SelectionListener
{
	private Player player; //Contains the game's player
	public ArrayList<EncounterAction> skills; //Contains the players skills
	private EncounterAction listenerAction; //Contains the action currently being cast
	
	private boolean isSelectingSkill = false; // flags to determine in which phase of an attack the player is
	private boolean isInBetween = false;
	private boolean isSelectingTarget = false;
	private Enemy target; //target of he attack
	private int   skillOption; //the chosen action
	 
	/**
	  * Contstructor 
	  * @param player The Represented player
	  */
	public EncounterPlayer(Player player)
	{
		this.player = player; 
		skills = player.skills; //load EncounterActions
		this.setMaxHP(player.maxHP); //sets maxHP
		this.setCurrentHP(player.currentHP);//sets currentHP
		this.setDefense(player.getArmorDefence()); //sets current Defense
				
		this.setName("Player"); //sets the player Name
		this.setBonusDamage(player.getWeaponDamage());//sets the players Bonus damage
		//myBattle = b;
	}
	
	/**
	 * Draw the players stats to the given image
	 * @param target
	 */
	public void onDraw(CharImage target)
	{
		//drawHP(target);
		HealthBar.draw(target, ASCIIStoryActivity.getScreenWidth()-19, ASCIIStoryActivity.getScreenHeight()-6, 18, this);
	}
	
	/**
	 * takeDamage function extended from EncounterCreature, synchronizes the actual players HP
	 */
	public void takeDamage(int damage, Battle b)
	{
		super.takeDamage(damage, b);
		player.currentHP = this.currentHP;
	}

	/**
	 * requestTarget function, called when the user has to select a target for the player
	 * @param a the Action requesting a target
	 * @return the target
	 */
	public Enemy requestTarget(EncounterAction a) 
	{
		return target;
	}

	/**
	 * Act Function for player, has two distinct phases, 
	 * in the first the player selects an Action, 
	 * int the second he selects a target
	 */
	public void act()
	{		
		if (!isSelectingSkill)
		{
			isSelectingSkill = true;
			OptionWindow.SelectionListener playerListener = new OptionWindow.SelectionListener()
			{
				public void select(int option) 
				{
					skillOption = option-1;
					isInBetween = true;
				}	
			};
			
			OptionWindow selectscreen  = new OptionWindow(1, 1, ASCIIStoryActivity.getScreenWidth()-2, 6, "Action", playerListener);
			for(EncounterAction a :player.skills) selectscreen.addOption(a.name);
		} 
		else if (isInBetween) 
		{
			if (!isSelectingTarget)
			{
				isSelectingTarget = true;
				ArrayList<Enemy> creatures = myBattle.getEnemies();
				OptionWindow selectscreen = new OptionWindow(1, 1, ASCIIStoryActivity.getScreenWidth()-2, 6, "Target", this);
				for (Enemy e: creatures) selectscreen.addOption(e.getName() + " ("+e.getCurrentHP()+"hp)");
			}
		}
	}
	
	/**
	 * select Function for Listener
	 */
	public void select(int option) 
	{
		target = myBattle.getEnemies().get(option-1);
		skills.get(skillOption).act(myBattle, this);
		
		isSelectingSkill 	= false;
		isInBetween 		= false;
		isSelectingTarget 	= false;
		
		myBattle.setEnemyTurn();
	}
}
